﻿using SIAKADCORE.Helpers;
using SIAKADCORE.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIAKADCORE.Controllers
{
    public static class DaftarRekomendasiController
    {
        public static bool GetTabel(out DataSet dataset)
        {
            string queryString = string.Format("SELECT {0}, {1}, {2} FROM {3}",
                Rekomendasi.ColumnNameIdRekomendasi, Rekomendasi.ColumnNameIsiRekomendasi, Rekomendasi.ColumnNameIdPertanyaan, Rekomendasi.TableName);
            return SQLHelper.Instance.ExecuteReader(queryString, out dataset);
        }

        public static bool GetTabel(out DataSet dataset, string customQueryString)
        {
            return SQLHelper.Instance.ExecuteReader(customQueryString, out dataset);
        }


        public static bool GetTabelPreview(out DataSet dataset, string idPertanyaan)
        {
            string queryString = string.Format("select rekomendasi.id_rekomendasi, pertanyaan.pertanyaan,   rekomendasi.rekomendasi from pertanyaan,  rekomendasi  where   rekomendasi.id_pertanyaan = pertanyaan.id_pertanyaan  and pertanyaan.id_pertanyaan='{0}'", idPertanyaan);
            return SQLHelper.Instance.ExecuteReader(queryString, out dataset);
        }

        public static string GetID()
        {
            var dataset = new DataSet();
            bool isQueryAccomplished = GetTabel(out dataset);
            if (!isQueryAccomplished) return string.Empty;

            return SQLHelper.Instance.GetID(Rekomendasi.IdPrefix, Rekomendasi.IdLength, ref dataset);
        }

        public static bool Tambah(Rekomendasi rekomendasi)
        {
            string queryString = String.Format("INSERT INTO {0} ({1}, {2}, {3}) VALUES ('{4}', '{5}', '{6}')",
                Rekomendasi.TableName, Rekomendasi.ColumnNameIdRekomendasi, Rekomendasi.ColumnNameIdPertanyaan, Rekomendasi.ColumnNameIsiRekomendasi, rekomendasi.IdRekomendasi, rekomendasi.IdPertanyaan, rekomendasi.IsiRekomendasi);
            return SQLHelper.Instance.ExecuteNonQuery(queryString);
        }

        public static bool Ubah(string IdDaftarRekomendasi, Rekomendasi rekomendasi)
        {
            string queryString = String.Format("UPDATE {0} SET {1} = '{2}', {3} = '{4}', {5} = '{6}' WHERE {1} = '{7}'",
                Rekomendasi.TableName, Rekomendasi.ColumnNameIdRekomendasi, rekomendasi.IdRekomendasi, Rekomendasi.ColumnNameIdPertanyaan, rekomendasi.IdPertanyaan, Rekomendasi.ColumnNameIsiRekomendasi, rekomendasi.IsiRekomendasi, IdDaftarRekomendasi);
            return SQLHelper.Instance.ExecuteNonQuery(queryString);
        }

        public static bool Hapus(string IdDaftarRekomendasi)
        {
            string queryString = string.Format("DELETE FROM {0} WHERE {1} = '{2}'", Rekomendasi.TableName, Rekomendasi.ColumnNameIdRekomendasi, IdDaftarRekomendasi);
            return SQLHelper.Instance.ExecuteNonQuery(queryString);
        }
    }
}
